<script setup lang="ts">
import { HomeWrap } from '@/components/HomeWrap'
import { addUser } from '@/api/user/client'
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'

defineOptions({
  name: 'UserRegister'
})

const formRef = ref<FormInstance>()
const router = useRouter()

const form = reactive({
  phone: '',
  password: '',
  name: '',
  branch: ''
})

const validatePhone = (rule: any, value: string, callback: any) => {
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!value) {
    callback(new Error('请输入手机号'))
  } else if (!phoneRegex.test(value)) {
    callback(new Error('手机号格式不正确'))
  } else {
    callback()
  }
}

const rules = {
  phone: [{ required: true, validator: validatePhone, trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度 6-20 位', trigger: 'blur' }
  ],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  branch: [{ required: true, message: '请输入部门', trigger: 'blur' }]
}

const submit = () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        // 调用注册 API
        const res = await addUser({
          phone: form.phone,
          password: form.password,
          name: form.name,
          branch: form.branch
        })

        if (res) {
          ElMessage.success('注册成功')
          // 注册成功后跳转到用户登录页面
          router.push('/user-login')
        }
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error('注册失败')
      }
    }
  })
}

const goToLogin = () => {
  router.push('/user-login')
}
</script>

<template>
  <HomeWrap>
    <div class="register-card">
      <h2>用户注册</h2>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="电话" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            maxlength="11"
            type="tel"
            size="large"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
            size="large"
          />
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" size="large" />
        </el-form-item>

        <el-form-item label="部门" prop="branch">
          <el-input v-model="form.branch" placeholder="请输入部门" size="large" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="large" style="width: 100%" @click="submit">
            注册
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="info" size="large" style="width: 100%" @click="goToLogin">
            已有账号，去登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </HomeWrap>
</template>

<style scoped>
.register-card {
  margin-top: 40px;
  padding: 24px;
}

h2 {
  text-align: center;
  margin-bottom: 24px;
}

:deep(.el-input) {
  height: 50px;
}

:deep(.el-input__inner) {
  height: 50px;
  font-size: 16px;
}
</style>